/**
 * Created by zhangqingxi on 2017-04-20.
 */
var modelPath=ROOT_PATH+'/environmental/monitor/point/';


//重置
function resetCombobox(id) {
    $(id).combobox({
        required:true,
        editable:false,
        prompt:'请选择',
        data:[]
    });
}

//初始化部门
function initDept() {

    $('#deptId').combotree({
        url: ROOT_PATH+'/permission/department/getDepartmentTree.shtml?parentId=0',
        required:true,
        editable:false,
        prompt:'请选择',
        onChange:function (newValue,oldValue) {
            $('#deviceId').combobox({
                value:''
            });
            initDevice(newValue);
        }
    });
}
//部门加载完成之后的事件
function deptLoadSuccess() {

    var $deptVal= ($('#deptId').val());
    initDevice($deptVal);
    //加载部门
    initDept();

}

//控制级别
function initControlLevel() {
    $('#controlLevelId').combobox({
        url:dictionaryByCode+controlLevel,
        valueField:'id',
        textField:'text',
        required:true,
        editable:false,
        prompt:'请选择'
    });
}

//监测点类型
function initMonitoringPointType() {
    $('#monitoringPointTypeId').combobox({
        url:dictionaryByCode+monitoringPointType,
        valueField:'id',
        textField:'text',
        required:true,
        editable:false,
        prompt:'请选择'
    });
}
//监测点位置
function initMonitoringPointPlace() {
    $('#monitoringPointPlaceId').combobox({
        url:dictionaryByCode+monitoringPointPlace,
        valueField:'id',
        textField:'text',
        required:true,
        editable:false,
        prompt:'请选择'
    });
}
//排放规律
function initEmissionLaw() {
    $('#emissionLawId').combobox({
        url:dictionaryByCode+emissionLaw,
        valueField:'id',
        textField:'text',
        required:true,
        editable:false,
        prompt:'请选择'
    });
}
//初始化污染物类型
function initContaminantsType() {
    $('#contaminantsTypeId').combobox({
        url:dictionaryByCode+contaminantsType,
        valueField:'id',
        textField:'text',
        required:true,
        editable:false,
        prompt:'请选择'
    });
}

//初始化 装置deviceId
function initDevice(deptId) {
    $('#deviceId').combobox({
        url:deviceByDeptId+deptId,
        prompt:'请选择',
        required:true,
        editable:false,
        valueField:'id',
        textField:'text'
    });
}
//初始化有效状态
function initEffectiveState() {

    $('#effectiveState').combobox({
        data:[{"id":0,"text":"无效"},{"id":1,"text":"有效"}],
        valueField:'id',
        textField:'text',
        prompt:'请选择',
        required:true,
        editable:false
    });
}
//初始化分析频次数据
function initFrequency() {

    $.ajax({
        url: dictionaryByCode+frequency,
        type:'POST',
        dataType:'json',
        async:false,
        success: function (data) {
            frequencyData=data;
        }
    });

}
//初始化 监测项目数据
function initProject() {

    $.ajax({
        url: findAllProject,
        type:'POST',
        dataType:'json',
        async:false,
        success: function (data) {
            projectData=data;
        }
    });

}
//格式化分析频次
function frequencyFormatter(value, rowData, rowIndex) {

    for (var i = 0; i < frequencyData.length; i++) {
        if (frequencyData[i].id == value) {
            return frequencyData[i].text;
        }
    }
}
//格式化监测项目
function projectFormatter(value, rowData, rowIndex) {

    for (var i = 0; i < projectData.length; i++) {
        if (projectData[i].id == value) {
            return projectData[i].text;
        }
    }
}

function  loadPointAndProjectList() {

    var pointId='';
    var $pointId=$('#pointId');
    if($pointId.length>0){
        pointId=$pointId.val();
    }
    $('#monitorProjectList').datagrid({
        url:findProjectByPointId+pointId,
        fitColumns:true,
        rownumbers:true,
        singleSelect:true,
        sortOrder:true,
        striped:true,
        fit:true,
        columns:[[
            {field:'id',title:'操作',width:10,formatter:function (value,row,index) {
                var html='<a href="javascript:void(0)" onclick="deleteRowByRowIndex(\'#monitorProjectList\')">删除</a>';
                html+='&nbsp;&nbsp;&nbsp;&nbsp;';
                html+='<a href="javascript:void(0)" onclick="editRowByRowIndex(\'#monitorProjectList\')">编辑</a>';
                html+='&nbsp;&nbsp;&nbsp;&nbsp;';
                html+='<a href="javascript:void(0)" onclick="endRowByRowIndex(\'#monitorProjectList\')">结束编辑</a>';

                return html;
            }},
            {field:'monitorProjectId',title:'监测点项目',width:10,
                editor:{
                    type:'combobox',
                    options: {
                        required: true,
                        editable: false,
                        valueField:'id',
                        textField:'text',
                        data:projectData,
                        prompt:'请选择'
                    }
                },
                formatter:projectFormatter
            },
            {field:'frequency',title:'分析频次',width:10,
                editor:{
                    type:'combobox',
                    options: {
                        required: true,
                        editable: false,
                        valueField:'id',
                        textField:'text',
                        data:frequencyData,
                        panelHeight:'auto',
                        prompt:'请选择'
                    }
                },
                formatter:frequencyFormatter
            },
            {field:'standardValueUpper',title:'标准值上限',width:10,
                editor:{
                    type:'numberspinner',
                    options: {
                        required: true,
                        precision:2,
                        min:0
                    }
                }
            },
            {field:'standardValueLower',title:'标准值下限',width:10,
                editor:{
                    type:'numberspinner',
                    options: {
                        required: true,
                        precision:2,
                        min:0
                    }
                }
            },
            {field:'unitOfMeasurement',title:'计量单位',width:10,
                editor:{
                    type:'text',
                    options: {
                        required: true
                    }
                }
            }
        ]],
        toolbar: '#monitorProjectToolbar'

    });


}

function initMonitorProjectButton() {

    $('#btn_monitorProject_add').on('click',function () {
        var $monitorProjectList=$('#monitorProjectList');
        $monitorProjectList.datagrid('insertRow',{
            row: {
                id: '',
                monitorProjectId: '',
                standardValueUpper: '',
                standardValueLower:'',
                unitOfMeasurement:''
            }
        });
        //获得总行数
        var rowsLength=$monitorProjectList.datagrid('getRows').length;
        $monitorProjectList.datagrid('beginEdit',rowsLength-1);
    })
}

function initSubmitButtonClick(url) {
    //表单提交
    $('#btn_save').on('click',function () {

        var index=layer.confirm('是否确定保存？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            $('#submit_form').form('submit', {
                url: url,
                onSubmit: function (param) {
                    var isValid = $(this).form('validate');
                    if (!isValid) {
                        layer.close(index);
                        layer.msg("请正确填写表单")
                    }
                    //获得单位表格数据
                    var monitorProjectRow=$('#monitorProjectList').datagrid('getRows');
                    param['monitorProject']=JSON.stringify(monitorProjectRow);
                    return $(this).form('validate');
                },
                success: function (result) {
                    var data = $.parseJSON(result);
                    successCallback(data);
                }
            });
        });

    });
}

/**
 * 删除当前行
 * @param id
 */
function deleteRowByRowIndex(id) {
    var selectRow=$(id).datagrid('getSelected');
    var selectRowIndex=$(id).datagrid('getRowIndex',selectRow);

    $(id).datagrid('deleteRow',selectRowIndex);
}
/**
 * 编辑当前行
 * @param id
 */
function editRowByRowIndex(id) {
    var selectRow=$(id).datagrid('getSelected');
    var selectRowIndex=$(id).datagrid('getRowIndex',selectRow);
    $(id).datagrid('beginEdit',selectRowIndex);
}
/**
 * 结束编辑当前行
 * @param id
 */
function endRowByRowIndex(id) {
    var selectRow=$(id).datagrid('getSelected');
    var selectRowIndex=$(id).datagrid('getRowIndex',selectRow);
    $(id).datagrid('endEdit',selectRowIndex);
}